package com.xindacloud.sms.console.system.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.xindacloud.sms.console.constant.SessionKeys;
import com.xindacloud.sms.console.result.Result;
import com.xindacloud.sms.console.result.ResultGenerator;
import com.xindacloud.sms.console.system.dto.ModifySmsAgentDTO;
import com.xindacloud.sms.console.system.dto.SaveSmsAgentDTO;
import com.xindacloud.sms.console.system.dto.SmsAgentDTO;
import com.xindacloud.sms.console.system.dto.UpdatePriceByCountryDTO;
import com.xindacloud.sms.console.system.entity.SmsAgent;
import com.xindacloud.sms.console.system.entity.SmsPricing;
import com.xindacloud.sms.console.system.entity.SmsUser;
import com.xindacloud.sms.console.system.service.ISmsAgentService;
import com.xindacloud.sms.console.system.service.ISmsPricingService;
import com.xindacloud.sms.console.system.service.ISmsUserAllowOperatorService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Objects;

/**
 * <p>
 * 短信通道代理商 前端控制器
 * </p>
 *
 * @author czc
 * @since 2019-10-08
 */


@Api(tags = {"短信通道接口模块"})
@RestController
@RequestMapping("/api/v1/sms-agent")
@ApiModel(value = "短信通道")
public class SmsAgentController {

    @Autowired
    private ISmsAgentService smsAgentService;

    @Autowired
    private ISmsPricingService smsPricingService;

    @Autowired
    ISmsUserAllowOperatorService userAllowOperatorService;


    @ApiOperation(value = "短信平台列表", response = SmsAgent.class)
    @PostMapping(value = "/getSmsPlatformList")
    public Result getSmsPlatformList(@ApiParam(value = "查询短信平台DTO", name = "smsAgentDTO")
                                     @RequestBody SmsAgentDTO smsAgentDTO) {
        return smsAgentService.getSmsPlatformList(smsAgentDTO);
    }

    @ApiOperation(value = "所有短信列表", response = SmsAgent.class)
    @PostMapping(value = "/getALLSmsAgentList")
    public Result getALLSmsAgentList(@ApiParam(value = "查询短信平台DTO", name = "smsAgentDTO")
                                     @RequestBody SmsAgentDTO smsAgentDTO, HttpServletRequest request) {
        HttpSession session = request.getSession();
        SmsUser user = (SmsUser) session.getAttribute(SessionKeys.ACCOUNT);
        smsAgentDTO.setUserId(user.getUserId());
        return smsAgentService.getALLSmsAgentList(smsAgentDTO);
    }

    @ApiOperation(value = "添加短信平台")
    @PostMapping("/save")
    public Result addAgent(@ApiParam(value = "添加短信平台DTO", name = "smsAgentDTO")
                           @RequestBody SaveSmsAgentDTO smsAgentDTO) {
        final SmsAgent smsAgent = smsAgentService.addSmsAgent(smsAgentDTO);
        return ResultGenerator.genSuccessResult(smsAgent);
    }


    @ApiOperation(value = "修改短信平台")
    @PutMapping("/modify")
    public Result modifyAgent(@ApiParam(value = "修改短信通道DTO", name = "modifySmsAgentDTO")
                              @RequestBody ModifySmsAgentDTO modifySmsAgentDTO) {
        final Boolean modifySmsAgent = smsAgentService.modifySmsAgent(modifySmsAgentDTO);
        return ResultGenerator.genSuccessResult(modifySmsAgent);
    }

    @ApiOperation(value = "短信平台导入运营商价格表(后缀为.xls的Excel文件)", notes = "返回200为修改成功,其他返回码均为异常")
    @PostMapping(value = "/agents/import")
    public Result importPriceExcel(@ApiParam(name = "file", value = "文件", required = true)
                                   @RequestParam("file") MultipartFile file,
                                   @ApiParam(name = "agentId", value = "代理商ID", required = true)
                                   @RequestParam(value = "agentId", name = "agentId") String agentId
    ) {
        return smsPricingService.importPriceExcel(file, agentId);
    }

    @ApiOperation(value = "查询短信平台运营商价格列表", response = SmsPricing.class)
    @GetMapping(value = "/agents/{agentId}/pages/{pageNum}/{pageSize}")
    public Result queryPriceList(@ApiParam(name = "agentId", value = "代理商ID", required = true)
                                 @PathVariable(value = "agentId", name = "agentId") String agentId,
                                 @ApiParam(name = "pageNum", value = "当前页数", required = true, example = "1")
                                 @PathVariable(value = "pageNum", name = "pageNum") Integer pageNum,
                                 @ApiParam(name = "pageSize", value = "每页显示条数", required = true, example = "10")
                                 @PathVariable(value = "pageSize", name = "pageSize") Integer pageSize,
                                 @ApiParam(name = "countryName", value = "国家名称")
                                 @RequestParam(value = "countryName", name = "countryName", required = false) String countryName,
                                 @ApiParam(name = "countryCode", value = "国家简称")
                                 @RequestParam(value = "countryCode", name = "countryCode", required = false) String countryCode) {
        final IPage<SmsPricing> smsPricingIPage = smsPricingService.queryPricingPageByAgentId(pageNum, pageSize, agentId, countryName, countryCode);
        return ResultGenerator.genSuccessResult(smsPricingIPage);
    }


    @ApiOperation(value = "根据国家修改价格", response = SmsPricing.class)
    @PutMapping(value = "/prices/country")
    public Result updatePriceByCountry(@ApiParam(name = "priceByCountryDTO", value = "根据国家更新价格")
                                       @RequestBody UpdatePriceByCountryDTO priceByCountryDTO) {

        if (Objects.isNull(priceByCountryDTO)) {
            return ResultGenerator.genClientFailResult("修改价格参数为空.");
        }
        if (StringUtils.isEmpty(priceByCountryDTO.getAgentId())) {
            return ResultGenerator.genClientFailResult("代理商Id不能为空.");
        }
        if (StringUtils.isEmpty(priceByCountryDTO.getCountryCode())
                && StringUtils.isEmpty(priceByCountryDTO.getCountryName())) {
            return ResultGenerator.genClientFailResult("修改价格请先根据国家名称或者国家简称过滤进行修改.");
        }

        if (priceByCountryDTO.getCostPrice() < 0) {
            return ResultGenerator.genClientFailResult("成本价不能小于0.");
        }
        if (priceByCountryDTO.getDefaultPrice() < 0) {
            return ResultGenerator.genClientFailResult("默认价不能小于0.");
        }

        final Boolean pricingByCountry = smsPricingService.updatePricingByCountry(priceByCountryDTO);
        userAllowOperatorService.updateUserAllowOperatorFor(priceByCountryDTO);
        return pricingByCountry ? ResultGenerator.genSuccessResult("修改成功")
                : ResultGenerator.genClientFailResult("修改失败");
    }

    @ApiOperation(value = "删除短信平台")
    @PostMapping("/deleteAgents")
    public Result deleteAgents(@ApiParam(value = "添加短信平台DTO", name = "smsAgentDTO")
                               @RequestBody SmsAgentDTO smsAgentDTO) {
        return smsAgentService.deleteAgents(smsAgentDTO);
    }

    @ApiOperation(value = "修改代理商状态")
    @PostMapping("/updateAgentStatus")
    public Result updateAgentStatus(@ApiParam(value = "添加短信平台DTO", name = "smsAgentDTO")
                                    @RequestBody SmsAgentDTO smsAgentDTO) {
        return smsAgentService.updateAgentStatus(smsAgentDTO);
    }


}
